package com.example.dyy.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.dyy.util.DBUtil;

@WebServlet("/movie/update")
public class MovieUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        int duration = Integer.parseInt(request.getParameter("duration"));
        String type = request.getParameter("type");
        String description = request.getParameter("description");
        double price = Double.parseDouble(request.getParameter("price"));
        
        String sql = "UPDATE movie SET name=?, duration=?, type=?, description=?, price=? WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, name);
            pstmt.setInt(2, duration);
            pstmt.setString(3, type);
            pstmt.setString(4, description);
            pstmt.setDouble(5, price);
            pstmt.setInt(6, id);
            
            pstmt.executeUpdate();
            
            response.sendRedirect("list");
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServletException("更新电影信息失败", e);
        }
    }
} 